import { CHANGE_INPUT, ADD_LIST, DELETE_LIST } from './actionTypes'
const defaultState = {
  inputValue: 'write something...',
  listData: [
    '5点起床',
    '6点跑步',
    '7点吃饭',
    '8点上班'
  ]
}
export default (state = defaultState, action) => {
  console.log(state, action)
  //注意：reducer里只能接收state，不能直接改变state
  if (action.type === CHANGE_INPUT) {
    //所以接下来需要深度拷贝
    let newState = JSON.parse(JSON.stringify(state))
    newState.inputValue = action.value
    return newState
  }
  if (action.type === ADD_LIST) {
    //所以接下来需要深度拷贝
    let newState = JSON.parse(JSON.stringify(state))
    newState.listData.unshift(newState.inputValue)
    //置空输入框
    newState.inputValue = ''
    return newState
  }
  if (action.type === DELETE_LIST) {
    //所以接下来需要深度拷贝
    let newState = JSON.parse(JSON.stringify(state))
    //删除相关项
    // console.log(action.value)
    newState.listData.splice(action.value, 1)
    return newState
  }
  return state
}